<template>
	<view class="leave">
		<myHeader title="请假申请"></myHeader>
		<view class="form">
			<view class="input" style="margin-top: 30rpx;">
				<view class="input_title">
					<text style="color:#F00">*</text>
					请假人：
				</view>
				<view class="input_value">
					{{model.name}}
				</view>
			</view>
			<view class="input" @click="show=true">
				<view class="input_title">
					<text style="color:#F00">*</text>
					请假类别：
				</view>
				<view class="input_value">
					<!-- <u--input placeholder="请输入内容" v-model="model.leaveCategory" disabled></u--input> -->
					{{columns[0][model.leaveCategory].name}}
				</view>
			</view>
			<view class="input" @click="openStart">
				<view class="input_title">
					<text style="color:#F00">*</text>
					开始时间：
				</view>
				<view class="input_value">
					{{model.leaveStartDate}}
				</view>
			</view>
			<view class="input" @click="openEnd">
				<view class="input_title">
					<text style="color:#F00">*</text>
					结束时间：
				</view>
				<view class="input_value">
					{{model.leaveEndDate}}
				</view>
			</view>
			<view class="input">
				<view class="input_title">
					<text style="color:#ee2024">*</text>
					请假时长：
				</view>
				<view class="input_value">
					{{model.total}}天
				</view>
			</view>
			<view>
				<view style="width: 100%;">
					<text style="color:#ee2024">*</text>
					请假事由：
				</view>
				<view style="margin-top: 20rpx;padding-left: 10rpx;height: 180rpx;">
					<textarea v-model="model.leaveReason" placeholder="请输入请假事由" maxlength="100" />
				</view>
			</view>
			<view class="input">
				<view class="input_title">
					<text style="color:#ee2024">*</text>
					联系方式：
				</view>
				<view class="input_value">
					<u--input v-model="model.contactWay" placeholder="请输入联系方式"
						border="none"></u--input>
				</view>
			</view>
			<view class="input">
				<view class="input_title">
					请假委托人：
				</view>
				<view class="input_value">
					<u--input v-model="model.dutyDeputy" placeholder="请输入请假委托人"
						border="none"></u--input>
				</view>
			</view>
			<view class="btn">
				<u-button text="登录" type="primary" @click="submit"></u-button>
			</view>
		</view>
		<u-picker :show="show" :columns="columns" keyName="name" :closeOnClickOverlay="true" @close="show=false"
			@cancel="show=false" @confirm="selOk"></u-picker>
			
		<!-- 选择开始时间 -->
		<xp-picker ref="pickerStart" :yearRange="[2017,2030]" mode="ymdh"  @confirm="confirmStart" />
		
		<!-- 选择结束时间 -->
		<xp-picker ref="pickerEnd" :yearRange="[2017,2030]" mode="ymdh"  @confirm="confirmEnd" />
	</view>
</template>

<script>
	import myHeader from '../../component/myHeader.vue'
	import {
		addLeave
	} from '../../api/api.js'
	export default {
		data() {
			return {
				show: false,
				model: {
					name: "jeecg", // 请假人
					leaveCategory: 0, // 请假类别
					leaveStartDate: "", // 开始日期
					leaveEndDate: "", // 结束日期
					total: "0", // 总天数
					leaveReason: "", // 请假原因
					contactWay: "", //联系方式
					dutyDeputy: "", // 工作委托人
				},
				startHour:"",
				endHour:"",
				columns: [
					[{
							name: "未选择",
							id: 0
						},
						{
							name: "病假",
							id: 1
						},
						{
							name: "事假",
							id: 2
						},
						{
							name: "年假",
							id: 3
						},
						{
							name: "婚假",
							id: 4
						},
						{
							name: "产假",
							id: 5
						},
						{
							name: "丧假",
							id: 6
						},
						{
							name: "探亲假",
							id: 7
						},
						{
							name: "护理假",
							id: 8
						},
						{
							name: "其他",
							id: 9
						},
					]
				],
				rules: {
					'name': {
						type: 'string',
						required: true,
						message: '请填写姓名',
						trigger: ['blur', 'change']
					},
					'leaveCategory': {
						type: 'string',
						max: 1,
						required: true,
						message: '请选择请假类别',
						trigger: ['blur', 'change']
					},
				}
			};
		},
		components: {
			myHeader
		},
		methods: {
			selOk(val) {
				this.show = false
				if (!val.indexs[0]) return false
				this.model.leaveCategory = val.indexs[0]
			},
			openStart(){
				this.$refs.pickerStart.show()
			},
			confirmStart(e){
				let date=e.value.split(" ")
				this.startHour=date[1]
				if(this.startHour>=0 && this.startHour<12){
					this.model.leaveStartDate=date[0]+" 上午"
				}else{
					this.model.leaveStartDate=date[0]+" 下午"
				}
				
			},
			openEnd(){
				if(this.model.leaveStartDate==""){
					uni.showToast({
						title:"请先选择开始时间",
						icon:"error",
						duration:1000
					})
					return false
				}
				this.$refs.pickerEnd.show()
			},
			confirmEnd(e){
				let startDate=new Date( this.model.leaveStartDate.split(" ")[0]).getTime()
				let date=e.value.split(" ")
				this.endHour=date[1]
				/* 判 */
				if(startDate > new Date(date[0]).getTime()){
					uni.showToast({
						title:"结束时间不能小于开始时间",
						icon:"error",
						duration:1000
					})
					return false
				}else if(startDate == new Date(date[0]).getTime()){
					/* 计算总天数 */
					if(this.startHour==this.endHour){
						this.model.total=0.5
					}else if(this.startHour < this.endHour){
						this.model.total=1
					}
				}else{
					let totalDay=new Date(date[0]).getTime()-startDate
					this.model.total=totalDay / (1000 * 60 * 60 * 24);
				}
				
				/* 赋值 */
				if(this.endHour>=0 && this.endHour<12){
					this.model.leaveEndDate=date[0]+" 上午"
				}else{
					this.model.leaveEndDate=date[0]+" 下午"
				}
			},
			submit(){
				addLeave(this.model).then(res=>{
					console.log(res);
				})
			}
		}
	}
</script>

<style lang="scss">
	.form {
		width: 750rpx;
		padding: 0 40rpx;
		box-sizing: border-box;
		font-size: 30rpx;

		.input {
			line-height: 100rpx;
			display: flex;
			align-items: center;
			.input_title {
				width: 30%;
			}

			.input_value {
				flex: 1;
			}
		}
		.btn{
			margin-top: 40rpx;
		}
	}
</style>
